package classes;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface IHello {
    void sayHello();
}

class Hello implements IHello {

    @Override
    public void sayHello() {
        System.out.println("Hello, World!");
    }
}

class IHelloProxy implements InvocationHandler {

    private Object origin;

    public Object bind(Object origin) {
        this.origin = origin;
        return Proxy.newProxyInstance(getClass().getClassLoader(), origin.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("------ proxy ---------");
        return method.invoke(origin, args);
    }
}

public class DynamicProxyDemo {
    public static void main(String[] args) {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true);
        IHello proxy = (IHello) new IHelloProxy().bind(new Hello());
        proxy.sayHello();
    }
}
